package com.example.summary.controller;

import com.alibaba.fastjson.JSON;
import com.example.summary.spring.MyFactoryBeanService;
import com.sun.org.apache.xpath.internal.SourceTree;
import jdk.nashorn.internal.objects.annotations.Constructor;
import jodd.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.net.www.http.HttpClient;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;


/**
 * Created by wzn on 2019/1/16.
 */
@RestController
public class TestController {

    @Autowired
    MyFactoryBeanService myFactoryBeanService;

    @Value("${aaa}")
    public int x;

    ExecutorService executorService = Executors.newFixedThreadPool(1);

    /**
     * 初始化线程5，最大5，队列大小20，超出丢弃，并且抛异常
     */
    private static final ThreadPoolExecutor sendThreadPoolExecutor = new ThreadPoolExecutor(
            1,
            1,
            0L,
            TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(2),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy()
    );

    @RequestMapping("${bbb}")
    public void test() {
        executorService.execute(() -> {
            System.out.println(1);
        });
//        myFactoryBeanService = new
//        System.out.println(myFactoryBeanService.test());
        System.out.println(x);
    }

    @RequestMapping("/testbbb")
    public void testbbb() throws InterruptedException, ExecutionException, TimeoutException {
        FutureTask futureTask = new FutureTask(() -> {
            System.out.println("neibu begin");
            String response = TicketHttpUtil.postJson("http://localhost:9876/testaaa", "");
            return response;
        });

        sendThreadPoolExecutor.execute(futureTask);

        System.out.println("begin");
        System.out.println(futureTask.get(15, TimeUnit.SECONDS));
        System.out.println("end");
    }

    @Scheduled(cron = "${bbb}")
    public void test2() {
//        myFactoryBeanService = new
//        System.out.println(myFactoryBeanService.test());
        System.out.println(x);
    }

    @RequestMapping("/testOOM")
    public void testOOM() throws InterruptedException {
        List<Data> dataList = new ArrayList<>();
        while (true) {
            dataList.add(new Data());
        }
    }

    class Data {
        byte[] xxx = new byte[1];
    }

}
